home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
10,000 Great Games
/
10,000 Great Games.iso
/
Product
/
66
/
data1.cab
/
Source_Files
/
Src
/
Text.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
2000-01-16
|
3KB
|
173 lines
#include "stdafx.h"
#include <stdarg.h>
cText *texts = 0;
HFONT tinyfont = 0, normalfont = 0, largefont = 0;
cText::cText(cText **list)
{
add((cList **)list);
*string = 0;
font = normalfont;
color = RGB(255, 255, 255);
centered = FALSE;
rendered_image = new cImage();
rendered_bmp = 0;
}
cText::~cText()
{
delete rendered_image;
if (rendered_bmp != 0)
delete rendered_bmp;
}
void cText::set_text(char *_string)
{
if (_string == 0 || *_string == 0)
{
*string = 0;
set_image(0);
}
else if (!eq(_string, string))
{
strcpy(string, _string);
render_text();
}
}
void cText::set_color(int _color)
{
if (color != _color)
{
color = _color;
if (*string == 0)
set_image(0);
else
render_text();
}
}
void cText::set_font(HFONT _font)
{
if (font != _font)
{
font = _font;
if (*string == 0)
set_image(0);
else
render_text();
}
}
void cText::set_centered(int _centered)
{
centered = _centered;
}
void cText::render_text()
{
HDC hdc;
SIZE size;
int len;
// First delete old image
if (rendered_bmp != 0)
delete rendered_bmp;
// Get length of string
len = lstrlen(string);
// Get size of string
if (FAILED(surface->dds->GetDC(&hdc)))
error("Unable to get device context for getting text size");
SelectObject(hdc, font);
GetTextExtentPoint32(hdc, string, len, &size);
surface->dds->ReleaseDC(hdc);
// Allocate new bitmap of correct size
rendered_bmp = new cBMP(size.cx, size.cy, string);
rendered_image->bmp = rendered_bmp;
// Render string
if (FAILED(rendered_bmp->dds->GetDC(&hdc)))
error("Unable to get device context for rendering string");
SelectObject(hdc, font);
SetBkColor(hdc, MASK_COLOR);
SetTextColor(hdc, color);
ExtTextOut(hdc, 0, 0, 0, 0, string, len, 0);
rendered_bmp->dds->ReleaseDC(hdc);
// Make sure image is updated
set_image(0);
// Set rendered image to current image
set_image(rendered_image);
// Do alignment
if (centered)
rendered_image->origin_center();
else
rendered_image->origin_lefttop();
}
void init_fonts()
{
tinyfont = CreateFont(14, 8, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH, "Arial");
if (tinyfont == 0)
error("Unable to create tiny font");
normalfont = CreateFont(16, 10, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH, "Arial");
if (normalfont == 0)
error("Unable to create normal font");
largefont = CreateFont(20, 12, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, VARIABLE_PITCH, "Arial");
if (largefont == 0)
error("Unable to create large font");
}
void deinit_fonts()
{
if (tinyfont != 0)
{
DeleteObject(tinyfont);
tinyfont = 0;
}
if (normalfont != 0)
{
DeleteObject(normalfont);
normalfont = 0;
}
if (largefont != 0)
{
DeleteObject(largefont);
largefont = 0;
}
}